package com.framework.modules.common.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.framework.common.config.SystemConfigName;
import com.framework.common.model.config.ConfigPaymentRateMoney2Coin;
import com.framework.common.utils.DateUtils;
import com.framework.common.utils.QuickHelper;
import com.framework.modules.base.dao.*;
import com.framework.modules.base.entity.*;
import com.framework.modules.base.service.UserOrderService;
import com.framework.modules.base.service.UserPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service("baseService")
public class BaseService {

    @Autowired
    private UserOrderService userOrderService;

    @Autowired
    private UserBalanceDao userBalanceDao;

    @Autowired
    private UserGiftDao userGiftDao;

    @Autowired
    private GiftDao giftDao;

    @Autowired
    private UserVipinfoDao userVipinfoDao;

    @Autowired
    private UserPaymentService userPaymentService;

    /**
     * 订单成功更新订单状态
     * @param payOrderId
     * @param channelOrderNo
     * @return
     */
    public boolean baseUpdateStatusSuccess(String payOrderId, String channelOrderNo) {
        UserOrderEntity userOrderEntity=new UserOrderEntity();
        userOrderEntity.setId(payOrderId);
        if(channelOrderNo != null) userOrderEntity.setOutTradeNo(channelOrderNo);
        userOrderEntity.setState(1);
        userOrderEntity.setNotifyDate(DateUtils.getSecondTimestamp(new Date()));
        return userOrderService.update(userOrderEntity,new EntityWrapper<UserOrderEntity>().eq("id",payOrderId).eq("state",0));
    }

    /**
     * 订单成功插入用户收支表
     * @param userOrderEntity
     * @return
     */
    public boolean baseAddUserPayment(UserOrderEntity userOrderEntity) {
        UserPaymentEntity userPaymentEntity=new UserPaymentEntity();
        userPaymentEntity.setOrderId(userOrderEntity.getId());
        userPaymentEntity.setType(userOrderEntity.getType());
        userPaymentEntity.setUid(userOrderEntity.getUid().toString());
        userPaymentEntity.setOccurValue(userOrderEntity.getAmount());
        if("coin".equals(userOrderEntity.getObjectName())){
            userPaymentEntity.setRemark(userOrderEntity.getQuantity().toString());
        }else {
            userPaymentEntity.setRemark(userOrderEntity.getSubject());
        }
        userPaymentEntity.setState("1");
        userPaymentEntity.setAdddate(DateUtils.getSecondTimestamp(new Date()));
        return userPaymentService.insert(userPaymentEntity);
    }

    /**
     * 订单失败更新订单状态
     * @param payOrderId
     * @param channelOrderNo
     * @return
     */
    public boolean baseUpdateStatusFail(String payOrderId, String channelOrderNo) {
        UserOrderEntity userOrderEntity=new UserOrderEntity();
        userOrderEntity.setId(payOrderId);
        userOrderEntity.setState(2);
        if(channelOrderNo != null) userOrderEntity.setOutTradeNo(channelOrderNo);
        userOrderEntity.setNotifyDate(DateUtils.getSecondTimestamp(new Date()));
        return userOrderService.update(userOrderEntity,new EntityWrapper<UserOrderEntity>().eq("id",payOrderId).eq("state",0));
    }

    /**
     * 增加余额
     * @param uid
     * @param amount 充值金额
     * @return
     */
    public Integer baseAddBalance(Integer uid, BigDecimal amount,String platform) {
        ConfigPaymentRateMoney2Coin paymentRateMoney2Coin= QuickHelper.getConfigValue(SystemConfigName.PAY_RATE_MONEY2COIN,ConfigPaymentRateMoney2Coin.class);
        int rate;
        if("ios".equals(platform)){
            rate= paymentRateMoney2Coin.getIos();
        }else{
            rate=paymentRateMoney2Coin.getAndroid();
        }
        return userBalanceDao.addCoin(uid,amount.intValue()*rate);
    }

    /**
     * 更新礼物
     * @param uid
     * @param giftId
     * @return
     */
    public Integer baseAddGift(Integer uid, Integer giftId,Integer count) {
        UserGiftEntity userGiftEntity=new UserGiftEntity();
        userGiftEntity.setUid(uid);
        userGiftEntity.setGiftId(giftId);
        GiftEntity giftEntity=giftDao.selectById(giftId);
        userGiftEntity.setGiftPrice(giftEntity.getPrice());
        int nowTime=DateUtils.getSecondTimestamp(new Date());
        userGiftEntity.setAdddate(nowTime);
        userGiftEntity.setExpireDate(nowTime+giftEntity.getDateLimit());
        userGiftEntity.setQuantity(count);
        userGiftEntity.setState("1");
        return userGiftDao.insert(userGiftEntity);
    }

    /**
     * 更新vip
     * @param uid
     * @param month 月数
     * @return
     */
    public Integer baseUpdateVip(Integer uid,Integer month) {
        UserVipinfoEntity userVipinfoEntity=new UserVipinfoEntity();
        userVipinfoEntity.setUid(uid);
        userVipinfoEntity.setLastModifyDate(DateUtils.getSecondTimestamp(new Date()));
        UserVipinfoEntity entity=userVipinfoDao.selectById(uid);
        if(entity==null){
            userVipinfoEntity.setIsVip(1);
            userVipinfoEntity.setExpireDate(DateUtils.getSecondTimestamp(new Date())+24*60*60*month);
            userVipinfoDao.insert(userVipinfoEntity);
        }
        userVipinfoEntity.setIsVip(1);
        userVipinfoEntity.setExpireDate(userVipinfoEntity.getExpireDate()+24*60*60*month);
        return userVipinfoDao.updateById(userVipinfoEntity);
    }

}
